package com.wsoft.portal.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.portal.entity.PortalModuleSubEntity;
import com.wsoft.portal.manager.PortalModuleSubManager;
import com.wsoft.portal.mapper.PortalModuleSubMapper;
import com.wsoft.portal.query.sub.ModuleSubInsertQuery;
import com.wsoft.portal.query.sub.ModuleSubUpdateQuery;
import com.wsoft.portal.service.IPortalModuleSubService;
import com.wsoft.portal.utils.PortalUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Z001
 * @since 2024年12月27日 10:01:48
 */
@Service
public class PortalModuleSubServiceImpl extends ServiceImpl<PortalModuleSubMapper, PortalModuleSubEntity> implements IPortalModuleSubService {

    @Resource
    private PortalModuleSubManager subManager;
    @Resource
    private PortalUtil portalUtil;

    /**
     * 新增栏目
     * @param query
     * @return
     */
    @Override
    public boolean insertModuleSub(ModuleSubInsertQuery query) {
        boolean b = subManager.save(Convert.convert(PortalModuleSubEntity.class, query));
        if(b){
            portalUtil.updateExtendParentSub(query.getPortalModuleId());
        }
        return b;
    }

    /**
     * 修改栏目
     * @param query
     * @return
     */
    @Override
    public boolean updateModuleSub(ModuleSubUpdateQuery query) {
        boolean b = subManager.updateById(Convert.convert(PortalModuleSubEntity.class, query));
        if(b){
            portalUtil.updateExtendParentSub(query.getPortalModuleId());
        }
        return b;
    }

    /**
     * 栏目详情
     * @param id
     * @return
     */
    @Override
    public PortalModuleSubEntity getModuleSubInfo(Long id) {
        return subManager.getById(id);
    }

    /**
     * 移除栏目
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteModuleSub(Long id) {
        return subManager.removeById(id);
    }
}
